﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

[AddComponentMenu("Behavior/LifeTime")]
public class BhvLifeTime : MonoBehaviour
{
    [HideInInspector]
    public float lifeTime = 1.0f;

    [SerializeField]
    private float lifeTimeMax = 1.0f;

    [Tooltip("LifeTime 结束")]
    public UnityEvent onLifeTimeEnd;


    public enum DestoryMode
    {
        Destory,
        Hide,
    }

    public DestoryMode destoryMode;


    public void ResetLifeTime()
    {
        lifeTime = lifeTimeMax;
    }


    // Start is called before the first frame update
    void Start()
    {
        this.ResetLifeTime();
    }

    // Update is called once per frame
    void Update()
    {
        if (lifeTime >= 0)
        {
            lifeTime -= Time.deltaTime;
        }
        else
        {
            CheckRecovery();
        }
    }

    private void CheckRecovery()
    {
        //恢复生命周期最大值
        lifeTime = lifeTimeMax;
        if (onLifeTimeEnd != null)
        {
            onLifeTimeEnd.Invoke();
        }

        //销毁模式
        switch (destoryMode)
        {
            case DestoryMode.Destory:
                Destroy(gameObject);
                break;
            case DestoryMode.Hide:
                this.gameObject.SetActive(false);
                break;
        }



    }
}
